package chen.web.forum;

import java.util.Date;

import chen.web.IdEntity;
import chen.web.favorite.Favoritable;
import chen.web.favorite.Favorite;
import chen.web.user.Account;
import chen.web.widget.Widget;

/**
 * @author song
 *	讨论主题
 */
public class Subject extends IdEntity implements Favoritable{
	
	public static enum TopRank{one, two, three, four};
	
	private static final int Flag_Hide = 0x01;			//隐藏
	private static final int Flag_Forbid_Reply = 0x02;	//禁止回复
	private static final int Flag_Top_Level_1 = 0x04;		//置顶级别
	private static final int Flag_Top_Level_2 = 0x08;		//置顶级别
	//private static final int Flag_Need_Login = 0x10;		//需要登录才能查看
	
	private String title;		//标题
	private int flag = 0;
	private int viewCount = 0;			//浏览量
	private int replyCount = 0;			//回复数量
	private String content;		//内容
	private Date dateline;			//发布时间
	private ForumAccount author;		//作者
	private String ip;		
	private boolean digest = false;		//精华
	private boolean top = false;		//置顶
	private Category category;
	private Widget widget;
	private boolean fakeDelete;
	private boolean needChecking;

	public Subject(int id){
		super(id);
	}
	
	protected Subject(){
		super(0);
	}
	
	protected void setId(int id){
		super.id = id;
	}
	
	public Subject(Category category){
		super(0);
		setCategory(category);
	}

	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public ForumAccount getAuthor() {
		return author;
	}
	protected void setAuthor(ForumAccount author) {
		this.author = author;
	}
	
	/**
	 * 判断是否为隐藏主题
	 * @return	若是隐藏主题返回true, 否则返回false
	 */
	public boolean isHide(){
		return (flag & Flag_Hide) == Flag_Hide;
	}

	/**
	 * 设置是否为隐藏主题
	 * @param hide	true为隐藏主题，false不是
	 */
	protected void setHide(boolean hide){
		if(hide)
			flag = flag | Flag_Hide;
		else 
			flag = flag & (~Flag_Hide);
	}
	
	/**
	 * 判断是否禁止回复
	 * @return	true禁止回复，false非禁止
	 */
	public boolean isForbidReply(){
		return (flag & Flag_Forbid_Reply) == Flag_Forbid_Reply;
	}
	
	/**
	 * 设置是否禁止回复
	 * @param forbid	true禁止回复，false非禁止
	 */
	protected void setForbidReply(boolean forbid){
		if(forbid)
			flag = flag | Flag_Forbid_Reply;
		else 
			flag = flag & (~Flag_Forbid_Reply);
	}

	protected void setReplyCount(int replyCount) {
		this.replyCount = replyCount;
	}

	public int getReplyCount() {
		return replyCount;
	}
	
	protected void setFlag(int flag) {
		this.flag = flag;
	}

	protected int getFlag() {
		return flag;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getTitle() {
		return title;
	}
	protected void setTop(boolean top) {
		this.top = top;
	}
	public boolean isTop() {
		return top;
	}
	
	protected void setTopRank(TopRank rank){
		switch(rank){
			case one : flag = (flag & (~Flag_Top_Level_1) & (~Flag_Top_Level_2)); break;
			case two : flag = (flag | Flag_Top_Level_1 & (~Flag_Top_Level_2)); break;
			case three : flag = (flag | Flag_Top_Level_2 & (~Flag_Top_Level_1)); break;
			case four : flag = (flag | Flag_Top_Level_1 | Flag_Top_Level_2); break;
		}
	}
	
	public TopRank getTopRank(){
		return getTopRank(flag);
	}
	
	protected void setViewCount(int viewCount) {
		this.viewCount = viewCount;
	}
	public int getViewCount() {
		return viewCount;
	}
	protected void setDateline(Date dateline) {
		this.dateline = dateline;
	}
	public Date getDateline() {
		return dateline;
	}
	protected void setIp(String ip) {
		this.ip = ip;
	}
	public String getIp() {
		return ip;
	}
	protected void setDigest(boolean digest) {
		this.digest = digest;
	}
	public boolean isDigest() {
		return digest;
	}
	
	
	static TopRank getTopRank(int flag){
		int bit1 = flag & Flag_Top_Level_1;
		int bit2 = flag & Flag_Top_Level_2;
		if(bit1 == 0 && bit2 == 0)
			return TopRank.one;
		else if(bit1 == Flag_Top_Level_1 && bit2 == 0)
			return TopRank.two;
		else if(bit1 == 0 && bit2 == Flag_Top_Level_2)
			return TopRank.three;
		else 
			return TopRank.four;
	}
	protected void setCategory(Category category) {
		this.category = category;
	}
	public Category getCategory() {
		return category;
	}
	public void setWidget(Widget widget) {
		this.widget = widget;
	}
	public Widget getWidget() {
		return widget;
	}

	@Override
	public Favorite favor() {
		// TODO Auto-generated method stub
		Favorite favorite = new Favorite(this);
		favorite.setTitle(getTitle());
		favorite.setDescription("fsdafsdfdas");
		return favorite;
	}

	protected void setNeedChecking(boolean needChecking) {
		this.needChecking = needChecking;
	}

	public boolean isNeedChecking() {
		return needChecking;
	}

	protected void setFakeDelete(boolean fakeDelete) {
		this.fakeDelete = fakeDelete;
	}

	public boolean isFakeDelete() {
		return fakeDelete;
	}

}
